""" master del TP de paralelos """

import pypvm
import sys
import os
from signal import SIGTERM
from sys import stdout
from time import sleep


def Parallel_minimax():

    slave_num = 0 # mientras tanto, algo hay que hacer despues
    start_slaves(slave_num)
    # normal minimax

    moves_list = realizar_jugada(tablero, turno) # retorna las posibles jugadas
    for i in moves_list:
        if (len(moves_list) == 1):
            #  master evaluates move using normal minimax calls
            pass # mientras tanto
        else:
            for j in slave_num:
                send_game(j, i)
                # moves_list.length = moves_list.length + 1
                # no se que esta linea anterior
            for j in slave_num:
                receive_game(j)
                process_results(slave_message)
    end_slaves(slave_num)


if __name__ == "__main__":
    try:
        # ============================ test mytid () ===========================
        my_tid = pypvm.mytid()
        print "MASTER: mi tid = %s" % my_tid
        # print pypvm.mytid().__doc__
        
        # ============================ test get/setopt () ======================
        print "MASTER: testing get/setopt ()"
        val = pypvm.setopt(pypvm.opt['Route'], pypvm.getopt (pypvm.opt['Route']))
        print "PvmRoute =", val

        # ============================ test catchout () ========================
        print "MASTER: setting catchout to stdout..."
        pypvm.catchout(stdout)

        # ============================ test spawn () ===========================
        print "MASTER: spawning 2 tasks (slave.py)..."
        tids = pypvm.spawn(os.getcwd() + "/slave.py", [], pypvm.spawnOpts['TaskDefault'], "", 2)
        print "MASTER: spawned task's tid = %s" % tids
        
        # ============================ test notify () ==========================
        print "MASTER: asking for notification on child exits..."
        pypvm.notify (pypvm.notifyDict['TaskExit'], 2, tids, 1)

        # ============================ test initsend () ========================
        print "MASTER: initializing message buffer to PvmDataDefault..."
        pypvm.initsend (pypvm.data['default'])

        # ============================ test mkbuf () ===========================
        print "MASTER: testing mkbuf ()..."
        newrbuf = pypvm.mkbuf (pypvm.data['default'])
        newsbuf = pypvm.mkbuf (pypvm.data['default'])

        # ============================ test get[rs]buf () ======================
        print "MASTER: testing get[rs]buf ()..."
        oldrbuf = pypvm.getrbuf ()
        oldsbuf = pypvm.getsbuf ()

        # ============================ test set[rs]buf () ======================
        print "MASTER: testing set[rs]buf ()..."
        pypvm.setrbuf (newrbuf)
        pypvm.setsbuf (newsbuf)

        # ============================ test freebuf () =========================
        print "MASTER: testing freebuf ()..."
        pypvm.freebuf (oldrbuf)
        pypvm.freebuf (oldsbuf)

        # ============================ test pk () ==============================
        data = {-1: [1, (2, 3, 4), 4]}
        print ("MASTER: packing %s..." % data)
        pypvm.pk (data)

        # ============================ test send () ============================
        for recipient in tids:
            print ("MASTER: sending data to tid %s..." % recipient)
            pypvm.send (recipient, 1)

        # ============================ test mcast () ===========================
        print "MASTER: initializing message buffer to PvmDataDefault..."
        pypvm.initsend (pypvm.data['default'])

        data = ['1', '2', '3']
        print ("MASTER: packing %s using pkbyte ()..." % data)
        pypvm.pkbyte (data, 1)

        data = [1.0123456789, -2, 3.14]
        print ("MASTER: packing %s using pkdouble ()..." % data)
        pypvm.pkdouble (data, 1)

        data = [1.0123456789, -2, 3.14]
        print ("MASTER: packing %s using pkfloat ()..." % data)
        pypvm.pkfloat (data, 1)

        data = [1, -2, 3]
        print ("MASTER: packing %s using pkint ()..." % data)
        pypvm.pkint (data, 1)

        data = [1, -2, 3]
        print ("MASTER: packing %s using pklong ()..." % data)
        pypvm.pklong (data, 1)

        data = [1, -2, 3]
        print ("MASTER: packing %s using pkshort ()..." % data)
        pypvm.pkshort (data, 1)

        pypvm.mcast (tids, 2)

        # ============================ test config () ==========================
        print "MASTER: results from config:", pypvm.config (1)

        # ============================ test tasks () ===========================
        print "MASTER: results from tasks:", pypvm.tasks (0)

        # ============================ test tidtohost () =======================
        print "MASTER: results from tidtohost:", pypvm.tidtohost (my_tid)

        # ============================ test pypvm exception ====================
        try:
            pypvm.send (-1, 0)
            print "MASTER: deliberate error didn't throw and exception (bad!)"
        except:
            print "MASTER: deliberate send exception caught"

        # ============================ sleep ===================================
        print "MASTER: sleeping for 2 seconds (so slaves can catch up)..."
        sleep (2)

        # ============================ test sendsig () =========================
        print "MASTER: sending SIGTERM to children..."
        for tid in tids:
            pypvm.sendsig (tid, SIGTERM)

        # ============================ test nrecv () ===========================
        print "MASTER: calling nrecv ()..."
        pypvm.nrecv (tids[0], 1)

        # ============================ test trecv () ===========================
        # NOTE: this also serves to sleep for 10 seconds, since no message is
        # coming.
        print "MASTER: calling trecv ()..."
        pypvm.trecv (10,tids[0],1)

        # ============================ test kill () ============================
        print "MASTER: killing slaves..."
        for tid in tids:
            pypvm.kill (tid)

        # ============================ receive exit notification ===============
        # NOTE: This is in conjunction with the notify test above.
        for tid in tids:
            pypvm.recv (-1, 2)
            print "MASTER: received an exit notification..."

        # ============================ test exit () ============================
        print "MASTER: exiting pvm..."
        pypvm.exit ()

    except:
        print "Un error pypvm ocurrio!"
        a =  sys.exc_info()
        print str(a[0])
        print a[1]
        #print sys.exc_info()
